package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.zr.model.Role;
import com.zr.service.RoleService;
import com.zr.service.impl.RoleServiceImpl;

/**
 * Servlet implementation class Role
 */
public class Roles extends HttpServlet {
    private static final long serialVersionUID = 1L;
    RoleService roleService = new RoleServiceImpl();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Roles() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("utf8");
        PrintWriter printWriter = response.getWriter();
        List<Role> list = roleService.getRoles();
        JSONArray jsonArray = new JSONArray();
        for (Role role : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", role.getRoleId());
            jsonObject.put("text", role.getRoleName());
            jsonObject.put("selected", true);
            jsonArray.put(jsonObject);
        }
        printWriter.write(jsonArray.toString());


    }

}
